﻿using MySqlConnector;
using SqlKata.Compilers;
using SqlKata.Execution;

var connectionString = "Server=localhost; Database=SqlKataDemo;Port=3306;charset=utf8;uid=root;pwd=xxx;";
using (var connection = new MySqlConnection(connectionString))
{
    connection.Disposed += (sender, args) =>
    {
        Console.WriteLine("Disposed: 连接已关闭，回连接池了");
    };
    connection.StateChange += (sender, e) =>
    {
        Console.WriteLine($"StateChange: " + e.CurrentState);
    };
    Console.WriteLine($"初始状态{connection.State}");

    var db = new QueryFactory(connection, new MySqlCompiler());

    for (int i = 0; i < 100; i++)
    {
        Console.WriteLine();
        Console.WriteLine($"{i}--{connection.State}");
        var order = db.Query().From("order").FirstOrDefault();
        var product = db.Query().From("product").FirstOrDefault();
        Console.WriteLine($"{i}--{connection.State}");
    }

    Console.WriteLine();
    Console.WriteLine($"结束状态{connection.State}");
    Console.WriteLine();
}
Console.ReadLine();